﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EzDesk.EzAppApi.Utility.EzDeskAppApi;
using System.Web.Routing;
using EzDesk.EzAppApi.Utility.Models;
using EzDesk.EzAppApi.Utility;
using System.Text;

namespace EzDesk.EzAppApi.Web.Utility.Controllers
{
    public class EzAppController : Controller
    {
        protected IEzApiClient _apiClient;


        protected UserProfile CurrentUserProfile
        {
            get
            {
                return _apiClient.GetUserProfileById(CurrentUserId);
            }
        }

        protected SimpleUser CurrentSimpleUserInfo
        {
            get
            {
                return _apiClient.GetSimpleUserInfos(CurrentUserId.ToString()).FirstOrDefault();
            }
        }

        protected Guid CurrentUserId
        {
            get
            {
                return new Guid(HttpContext.Request["ez_user"]);
            }
        }

        protected override RedirectToRouteResult RedirectToAction(string actionName, string controllerName, RouteValueDictionary routeValues)
        {
            AppSession currentSession = EzDeskAppApiHelper.AppSessionHelper.GetCurrentSession();
            RouteValueDictionary routeValueDictionary = (RouteValueDictionary)routeValues;
            putSessionToRouteValues(currentSession, ref routeValueDictionary);

            return base.RedirectToAction(actionName, controllerName, routeValueDictionary);
        }

        protected virtual ActionResult ReturnView(string r, string txtInfo, object routeValues)
        {
            SetReturnInfoText(txtInfo);
            return View(routeValues);
        }

        protected ActionResult ReturnView(string actionName, string controllerName, string txtInfo, object routeValues)
        {
            SetReturnInfoText(txtInfo);
            return RedirectToAction(actionName, controllerName, routeValues);
        }

        protected void SetReturnInfoText(string txtInfo)
        {
            TempData["txtInfo"] = txtInfo;
        }

        private void putSessionToRouteValues(AppSession currentSession,ref RouteValueDictionary routeValues)
        {
            if (routeValues == null)
            {
                routeValues = new RouteValueDictionary();
            }
            routeValues["ez_user"] = currentSession.UserId;
        }



        /// <summary>
        /// 对Task的xml返回内容
        /// </summary>
        /// <param name="taskCollection"></param>
        /// <returns></returns>
        protected ActionResult Task(AppTaskCollection taskCollection)
        {
            return new ContentResult()
            {
                ContentType = "text/xml",
                ContentEncoding = Encoding.UTF8,
                Content = taskCollection.ToXmlString(),
            };
        }

    }


    public class EzDeskReturnInfo
    {
        public string State { get; set; }
        public string Info { get; set; }
        public dynamic Data { get; set; }
    }
}
